//
//  MAPYSettingViewController.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYSettingViewController: MAPYBaseCollectionViewController {

    // MARK: - mapy action
    override func mapy_layout() -> UICollectionViewFlowLayout {
        let mapy_layout = UICollectionViewFlowLayout()
        mapy_layout.itemSize = CGSize(width: mapy_SCREEN_WIDTH, height: 40)
        mapy_layout.headerReferenceSize = CGSize(width: mapy_SCREEN_WIDTH, height: 230)
        mapy_layout.minimumLineSpacing = 0
        return mapy_layout
    }
    override func mapy_collectionViews() {
        super.mapy_collectionViews()
        view.addSubview(mapy_navView)
        mapy_collectionView.snp.makeConstraints { make in
            make.left.width.bottom.equalToSuperview()
            make.top.equalTo(mapy_navView.snp.bottom)
        }
       
        mapy_collectionView.register(MAPYSettingHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: MAPYSettingHeadView.mapy_viewToString)
        mapy_collectionView.dataSource = self
        mapy_collectionView.delegate = self
        mapy_collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: MAPYCommon.mapy_tabbarH+15, right: 0)
        mapy_collectionView.register(MAPYSettingCell.classForCoder(), forCellWithReuseIdentifier: MAPYSettingCell.mapy_viewToString)
    }
    // MARK: - mapy lazy
    override func mapy_baseBgName() -> String? {
        return "mapy_setting_bg"
    }
    lazy var mapy_navView: MAPYNavView = {
        let mapy_navView = MAPYNavView()
        return mapy_navView
    }()
    lazy var mapy_settingModels: [MAPYSettingModel] = {
        var mapy_settingModels = [MAPYSettingModel]()
        mapy_settingModels.append(MAPYSettingModel(mapy_type: .mapy_rate,mapy_title: "Rate Us", mapy_icon: "mapy_setting_rate"))
        mapy_settingModels.append(MAPYSettingModel(mapy_type: .mapy_share,mapy_title: "Share", mapy_icon: "mapy_setting_share"))
        mapy_settingModels.append(MAPYSettingModel(mapy_type: .mapy_privacy,mapy_title: "Privacy Policy", mapy_icon: "mapy_setting_privacy"))
        return mapy_settingModels
    }()
}

extension MAPYSettingViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return mapy_settingModels.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYSettingCell.mapy_viewToString, for: indexPath) as! MAPYSettingCell
        mapy_cell.mapy_settingModel = mapy_settingModels[indexPath.row]
        return mapy_cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let mapy_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: MAPYSettingHeadView.mapy_viewToString, for: indexPath) as! MAPYSettingHeadView
        return mapy_headView
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let mapy_settingModel = mapy_settingModels[indexPath.row]
        if mapy_settingModel.mapy_type == .mapy_rate {
            MAPYManager.mapy_manager.mapy_switchAppStore()
            return
        }
        if mapy_settingModel.mapy_type == .mapy_share {
            MAPYManager.mapy_manager.mapy_shareApp(mapy_vc: self)
            return
        }
        navigationController?.pushViewController(MAPYSettingPrivacyViewController(), animated: true)
    }
}
